using System;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeUI;
using Jbe.TestSuite.Infrastructure.Interface;
using Jbe.TestSuite.TestDevice.FunctionGen.Interface.Services;
using Microsoft.Practices.CompositeUI.EventBroker;
using Jbe.TestSuite.TestDevice.FunctionGen.ControlView.Constants;

namespace Jbe.TestSuite.TestDevice.FunctionGen.ControlView.Views
{
    public partial class ConfigurationEditPresenter : Presenter<IConfigurationEdit>
    {
        [EventPublication(EventTopicNames.ConfigurationChanged, PublicationScope.WorkItem)]
        public event EventHandler ConfigurationChanged;

        private IFunctionGen functionGen;

        [ServiceDependency]
        public IFunctionGen FunctionGen
        {
            set { functionGen = value; }
        }

        public override void OnViewReady()
        {
            base.OnViewReady();
            View.Config = functionGen.Config.Clone();
        }

        public void OnCloseView()
        {
            base.CloseView();
        }

        public void Apply()
        {
            functionGen.Config = View.Config;
            OnConfigurationChanged();
        }

        protected virtual void OnConfigurationChanged() 
        {
            if (ConfigurationChanged != null) { ConfigurationChanged(this, EventArgs.Empty); }
        }
    }
}